package com.tl.mqtt.controller;

import com.alibaba.fastjson.JSONObject;
import com.tl.mqtt.model.*;
import com.tl.mqtt.mqtt.MqttPushClient;
import com.tl.mqtt.utils.DateUtil;
import com.tl.mqtt.utils.FileToBase64;
import com.tl.mqtt.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@EnableScheduling
@RequestMapping("/test")
public class TestController {

    private Logger logger = LoggerFactory.getLogger(TestController.class);
    private final String topic = "/v1/devices/202112011001/datas";
    private final Integer qos = 1;

    @Autowired
    private MqttPushClient client;

    /**
     * 模拟覆冰数据发送
     */
   // @Scheduled(cron = "1/5 * * * * ?")
    public void sendFbMsg(){
        logger.info("发送覆冰数据");
        LlfbPropertiesDto laLiFuBing = new LlfbPropertiesDto();
        laLiFuBing.setCs("shxy");
        laLiFuBing.setCjsj(DateUtil.FormatDateOne(new Date()));
        laLiFuBing.setSbid("86072304758305500");
        laLiFuBing.setSjrq("2021-12-22");
        laLiFuBing.setSjlx("FB");
        laLiFuBing.setBjzlc("0.5");
        laLiFuBing.setFbhd("0.5");
        laLiFuBing.setZxqxj("0.5");
        laLiFuBing.setHxqxj("0.5");
        laLiFuBing.setLlcgqs("0.5");
        laLiFuBing.setLlzsz("0.5");
        laLiFuBing.setZhzh("0.5");

        String content = handlObj("ICING","D4510771aIUmH",laLiFuBing);
        logger.info("发送的数据：{}",content);
        client.publish(qos,topic, content);
    }


    /**
     * 模拟微气象数据发送
     */
    //@Scheduled(cron = "1/5 * * * * ?")
    public void sendWqxMsg(){
        logger.info("发送微气象数据");
        WqxPropertiesDto sxdlweiqixiang = new WqxPropertiesDto();
        sxdlweiqixiang.setCs("shxy");
        sxdlweiqixiang.setCjsj(DateUtil.FormatDateOne(new Date()));
        sxdlweiqixiang.setSbid("86072304758305500");
        sxdlweiqixiang.setSjrq("2021-12-21");
        sxdlweiqixiang.setSjlx("WQX");
        sxdlweiqixiang.setBzfs("2000");
        sxdlweiqixiang.setFx("20.0");
        sxdlweiqixiang.setGfsqd("200");
        sxdlweiqixiang.setJdfs("200");
        sxdlweiqixiang.setJyl("200");
        sxdlweiqixiang.setJyqd("200");
        sxdlweiqixiang.setPjfs("200");
        sxdlweiqixiang.setQxgj("200");
        sxdlweiqixiang.setQy("200");
        sxdlweiqixiang.setSd("200");
        sxdlweiqixiang.setWd("200");
        sxdlweiqixiang.setZdfs("200");

        String content = handlObj("MICROMETE","D4510771aIUmH",sxdlweiqixiang);
        logger.info("发送的数据：{}",content);
        client.publish(qos,topic, content);
    }

    /**
     * 模拟舞动数据发送
     */
    //@Scheduled(cron = "1/5 * * * * ?")
    public void sendWdMsg(){
        logger.info("发送舞动数据");
        WdPropertiesDto sxdlwudong = new WdPropertiesDto();
        sxdlwudong.setCs("zx");
        sxdlwudong.setCjsj(DateUtil.FormatDateOne(new Date()));
        sxdlwudong.setSbid("86072304758305500");
        sxdlwudong.setSjrq("2021-12-21");
        sxdlwudong.setZzlx("123");
        sxdlwudong.setSjlx("WD");
        sxdlwudong.setCjdyxh("200");
        sxdlwudong.setCjdyzs("200");
        sxdlwudong.setCzfz("200");
        sxdlwudong.setSpfz("200");
        sxdlwudong.setTyj("200");
        sxdlwudong.setWdpl("200");
        sxdlwudong.setXzb("200");
        sxdlwudong.setYzb("200");
        sxdlwudong.setZzb("200");
        sxdlwudong.setZhfz("200");

        String content = handlObj("DANCE","D4510771aIUmH",sxdlwudong);
        logger.info("发送的数据：{}",content);
        client.publish(qos,topic, content);
    }


    /**
     * 模拟图像数据发送
     */
    @Scheduled(cron = "1/10 * * * * ?")
    public void sendTxMsg() throws IOException {
        logger.info("发送图像数据");
        String url = "https://i0.hdslb.com/bfs/article/c246d4474bad0e1e0e2c25419a74a2ab1d1454c3.jpg";
        String fileBase64Str = FileToBase64.fileToBase64(url);
        //获取分包列表
        List<String> list = FileUtils.strUnPacking(fileBase64Str, 1024*300);
        //分批发送图像数据至物管平台
        for (int i = 0; i < list.size(); i++) {
            //数据转换图像
            CgtxPropertiesDto tuxiang = new CgtxPropertiesDto();
            tuxiang.setSbid("86072304758305500");
            tuxiang.setCs("shyx");
            tuxiang.setSjlx("TX");
            tuxiang.setPackage_index(i+1); //排序号
            tuxiang.setPackage_num(list.size()); //总包号
            tuxiang.setPsy("自动抓拍");
            tuxiang.setSjrq("2021-12-21");
            tuxiang.setCjsj(DateUtil.FormatDateOne(new Date()));
            tuxiang.setTxmc(Math.random() * 1000 + System.currentTimeMillis() + ".jpg");
            tuxiang.setUrl(list.get(i)); //包数据

            String content = handlObj("PHOTO","D4510771aIUmH",tuxiang);
            logger.info("发送图像数据：{}","TX");
            client.publish(qos,topic, content);
        }
        list = null;
    }

    /**
     * 统一封装数据发送对象
     */
    public String handlObj(String serviceId,String iotId,Object obj){
        //封装services
        Services services = new Services();
        services.setData(obj);
        services.setServiceId(serviceId);
        services.setEventTime(DateUtil.FormatDate(new Date()));
        //封装List<Services>
        List<Services> list = new ArrayList<>();
        list.add(services);
        //封装MQTT上送对象
        Devices deviceS = new Devices();
        deviceS.setDeviceId(iotId);
        deviceS.setServices(list);
        //封装发送消息对象
        DataUploadRequest dataUploadRequest = new DataUploadRequest();
        dataUploadRequest.setDevices(new Devices[]{deviceS});
        return JSONObject.toJSONString(dataUploadRequest);
    }

}
